﻿using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;


public class Window : BaseMarkerUI
{
    public GameObject sliderObj = null;
    private RadialSlider radialSlider = null;
   // public MainMenuHandler mmh;

    //public ConView conView;
    protected float deltaAngle = 0;
    protected float preMarkerAngle = 0;
    public MainMenuHandler mmh;
    public GameObject center;

    public ConView conView;

    protected GameObject con;
    protected int curIndex = 0;
    protected int lastIndex = 0;
    protected Section curSection;
    protected float perAngle;
    public GameObject link;
    public SectionData sd;

    private bool canClick=true;
    void Start()
    {
        perAngle = 0.8f / 2f;//0.083333   0.16666   0.249999 0.33332 0.41665  0.49998
        con = conView.gameObject;
        con.SetActive(false);

        center.transform.DORotate(new Vector3(0, 0, 90), 1f).From();
        center.transform.DOScale(new Vector3(0.1f, 0.1f, 0.1f), 1f).From().OnComplete(showMenu);




    }

    public void setCanClick()
    {
        canClick=true;
    }

    protected void showMenu()
    {
      //  mmh.showBts();
        Invoke("showCon", 0.5f);
    }

    public void showCon()
    {
        con.SetActive(true);
    }


    public void initData(SectionData data)
    {
        sd = data;
        //Debug.Log("window initData");
        changeData();

    }

    public void ConRest()
    {

        con.SetActive(false);
    }



    void Awake()
    {
        if (sliderObj)
            radialSlider = sliderObj.GetComponent<RadialSlider>();
        if (radialSlider)
        {
           // radialSlider.ValueChangedCallback += OnValueChanged;
        }
    }

    public override void InitMarker(Marker m)
    {
        //Debug.Log("InitMarker");
        mMarker = m;
        RectTransform rt = GetComponent<RectTransform>();
       Debug.Log(marker.position);
        if (marker.position.y > 540f)
        {

            rt.rotation = Quaternion.Euler(0, 0, 180f);
        }
        preMarkerAngle = m.angle;
        UpdateUI();
    }
    public override void UpdateUI()
    {
        
      
        if (marker == null)
            return;

        transform.position = marker.position;
        deltaAngle = marker.angle - preMarkerAngle;
        preMarkerAngle = marker.angle;
        if (Mathf.Abs(deltaAngle) > 300f)
        {
            //处理0和360临界的情况
            deltaAngle += (deltaAngle > 0f ? -360f : 360f);
        }
        //Debug.LogFormat("Angle = {0}, delta = {1}",marker.angle,deltaAngle);
        value01 += (deltaAngle / 360.0f);


    }

    protected float value01
    {
        get { return radialSlider != null ? radialSlider.rawValue01 : 0; }
        set
        {
            if (radialSlider)
            {
                radialSlider.rawValue01 = value;
            }
        }
    }

    protected virtual void OnValueChanged(float value)
    {

      //  if (mmh == null) return;

        if (value01 > 0.5)
        {
            value01 -= 0.5f;
        }
        var t2 = perAngle * 2;
   

        if (value01 <= perAngle)
        {
            curIndex = 0;

        }
        else if (value01 <= t2)
        {
            curIndex = 1;
        }
       
     
        if (lastIndex == curIndex) return;


        lastIndex = curIndex;
        changeData();


    }

    public void handleMainMenuClick(int tag)
    {
         if(!canClick)return;
         canClick=true;
         Invoke("setCanClick",1.5f);

          if (curIndex == tag) return;


        lastIndex = curIndex;
        curIndex=tag;
          changeData();
    }

    protected void changeData()
    {
      
     //   Debug.Log("sd=" + sd);
       // Debug.Log("sd.data=" + sd.data);
        curSection = sd.data[curIndex];
        link.SetActive(false);
        conView.removeTreeBts();
        mmh.changeSection(curIndex, curSection);


    }


    // Update is called once per frame
    void Update()
    {

    }
}
